<?php
namespace App\Services;
use Log;

class AMap
{
	static $key ;

	public static function coordiateToAddress($lnglat='')
	{
		$rs['status'] = false;
		
		$url = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location='.$lnglat.'&key='.self::getKey();
		$regeo = file_get_contents($url);
		$data = json_decode($regeo,true);
		$zhixia_arr = ['北京市','上海市','天津市','重庆市'];
		//dd($data);
		if($data['info']=='OK') {
			$rs['status']  = true;
			$regeocode = $data['regeocode'];
			$rs['address'] = $regeocode['formatted_address'];
			$province = $regeocode['addressComponent']['province'];
			if(in_array($province, $zhixia_arr)) {
				$rs['city'] = $province;
			} else {
				$rs['city'] =$regeocode['addressComponent']['city'];
			}
			$rs['province'] = $province;
			
		} else {
			Log::info('地理位置解析失败:'.$data['info']);
		}
		
		
		
		return $rs;
		
	}

	

	private static function getKey()
	{
		return config('amap.key');
	}
	
}
	
?>